package com.ldkj.panzh.controller;

import com.ldkj.panzh.domain.AjaxResult;
import com.ldkj.panzh.param.PlcParam;
import com.ldkj.panzh.service.PlcPostService;
import com.ldkj.panzh.vo.PlcVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.text.SimpleDateFormat;
import java.util.Date;

@Controller
@RequestMapping("/plc")
public class PlCController extends BaseController {

    @Autowired
    PlcPostService plcPostService;

    /**
     * 获取设备PLC信息
     * @param plcParam
     * @return
     * @throws Exception
     */
    @RequestMapping("/getEqpPlcInfo")
    @ResponseBody
    public AjaxResult getEqpPlcInfo(@RequestBody PlcParam plcParam) throws Exception {

        String path = plcParam.getPath();
        // 检查路径是否有效
        if (path != null && !path.isEmpty()) {
            char lastChar = path.charAt(path.length() - 1);

            switch (lastChar) {
                case '1':
                    return success(buildPlcVO1(plcParam));
                case '2':
                    return success(buildPlcVO2(plcParam));
                case '3':
                    return success(buildPlcVO3(plcParam));
                default:
                    return success(plcPostService.getEqpPlcInfo(plcParam));
            }
        } else {
            return success(plcPostService.getEqpPlcInfo(plcParam));
        }


    }

    /**
     * 获取设备PLC信息
     * true 和 false
     * @param plcParam
     * @return
     * @throws Exception
     */
    @RequestMapping("/getEqpPlcInfo1")
    @ResponseBody
    public AjaxResult getEqpPlcInfo1(@RequestBody PlcParam plcParam) throws Exception {
        String path = plcParam.getPath();
        // 检查路径是否有效
        if (path != null && !path.isEmpty()) {
            char lastChar = path.charAt(path.length() - 1);

            switch (lastChar) {
                case '1':
                    return success(buildPlcVO1(plcParam));
                case '2':
                    return success(buildPlcVO2(plcParam));
                case '3':
                    return success(buildPlcVO3(plcParam));
                default:
                    return success(buildPlcVO1(plcParam));
            }
        } else {
            return success(buildPlcVO1(plcParam));
        }
    }

    /**
     * 获取设备PLC信息
     * 0 和 1
     * @param plcParam
     * @return
     * @throws Exception
     */
    @RequestMapping("/getEqpPlcInfo2")
    @ResponseBody
    public AjaxResult getEqpPlcInfo2(@RequestBody PlcParam plcParam) throws Exception {
        return success(buildPlcVO2(plcParam));
    }

    /**
     * 获取设备PLC信息
     * "" 和 null
     * @param plcParam
     * @return
     * @throws Exception
     */
    @RequestMapping("/getEqpPlcInfo3")
    @ResponseBody
    public AjaxResult getEqpPlcInfo3(@RequestBody PlcParam plcParam) throws Exception {
        return success(buildPlcVO3(plcParam));
    }


    /** true 和 false*/
    private PlcVO buildPlcVO1(PlcParam plcParam) {
        PlcVO vo = new PlcVO();


        // 生成随机布尔值
        boolean randomBoolean = Math.random() < 0.5;
        vo.setValue(String.valueOf(randomBoolean));


        // 当前时间
        vo.setTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));

        // 设置 path
        vo.setPath(plcParam.getPath());

        return vo;
    }

    /** 0 和 1*/
    private PlcVO buildPlcVO2(PlcParam plcParam) {
        PlcVO vo = new PlcVO();

        // 直接生成随机0或1（概率各约50%）
        int randomInt = (int)(Math.random() * 2);
        vo.setValue(String.valueOf(randomInt));

        // 当前时间
        vo.setTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));

        // 设置 path
        vo.setPath(plcParam.getPath());

        return vo;
    }

    /** "" 和 null */
    private PlcVO buildPlcVO3(PlcParam plcParam) {
        PlcVO vo = new PlcVO();

        // 随机生成空字符串或null（各50%概率）
        if (Math.random() < 0.5) {
            vo.setValue("");  // 空字符串
        } else {
            vo.setValue(null);  // null
        }

        // 当前时间
        vo.setTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));

        // 设置 path
        vo.setPath(plcParam.getPath());

        return vo;
    }

}
